﻿using System;
using System.Windows.Forms;

namespace lab05
{
    public partial class Form1 : Form
    {
        private BwWithShadows _bwWithShadows;
        private BwWithoutShadows _bwWithoutShadows;
        private ColorWithShadows _colorWithShadows;
        private ColorWithoutShadows _colorWithoutShadows;
        private bool _isDraw;
        private MatrixVisualizator _visualizator;
        private IMatrixDrawer _matrixDrawer;
        private ManagerCommand _managerCommand;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1Load(object sender, EventArgs e)
        {
            _isDraw = false;
            _colorWithoutShadows = new ColorWithoutShadows {Panel = panel1};
            _colorWithShadows = new ColorWithShadows {Panel = panel1};
            _bwWithoutShadows = new BwWithoutShadows {Panel = panel1};
            _bwWithShadows = new BwWithShadows {Panel = panel1};
            _matrixDrawer = _colorWithoutShadows;
            _managerCommand = new ManagerCommand();
        }

        private void DrawMe()
        {
            _visualizator.Drawer = _matrixDrawer;
            if (!_isDraw) return;
            _visualizator.Visualize();
        }

        private void Button1Click(object sender, EventArgs e)
        {
            IMatrix matrix = new DenseMatrix(7, 8);

            _managerCommand = new ManagerCommand();

            InitMatrix cmd = new InitMatrix(matrix, 7 * 8, 1, 10, _managerCommand);
            cmd.Execute();

            _visualizator = new MatrixVisualizator(matrix, _matrixDrawer);
            _isDraw = true;
            
            DrawMe();
        }

        private void ChangeClick(object sender, EventArgs e)
        {
            Random rnd = new Random();

            int i = rnd.Next(_visualizator.Matrix.GetNumRows());
            int j = rnd.Next(_visualizator.Matrix.GetNumColumns());
            int value = 100 + rnd.Next(900);

            MatrixChange cmd = new MatrixChange(_visualizator.Matrix, i, j, value, _managerCommand);
            cmd.Execute();

            DrawMe();
        }

        private void UndoClick(object sender, EventArgs e)
        {
            _managerCommand.isActive = false;
            _managerCommand.Undo();
            _managerCommand.isActive = true;
            DrawMe();
        }

        private void RadioButton1CheckedChanged(object sender, EventArgs e)
        {
            _matrixDrawer = _colorWithoutShadows;
            DrawMe();
        }

        private void RadioButton2CheckedChanged(object sender, EventArgs e)
        {
            _matrixDrawer = _colorWithShadows;
            DrawMe();
        }

        private void RadioButton3CheckedChanged(object sender, EventArgs e)
        {
            _matrixDrawer = _bwWithoutShadows;
            DrawMe();
        }

        private void RadioButton4CheckedChanged(object sender, EventArgs e)
        {
            _matrixDrawer = _bwWithShadows;
            DrawMe();
        }
    }
}